package com.fjut.zr.judge_system.Controller;

import com.fjut.zr.judge_system.Config.interceptor.annotation.LogAnnotation;
import com.fjut.zr.judge_system.OJ.Submitter.OJSubmitter;
import com.fjut.zr.judge_system.OJ.Submitter.OJSubmitterImpl;
import com.fjut.zr.judge_system.POJO.VO.OJSubmitinfo;
import com.fjut.zr.judge_system.POJO.PO.TVjSubmitPO;
import com.fjut.zr.judge_system.Util.CodeLanguage;
import com.fjut.zr.judge_system.Util.ResultJson;
import com.fjut.zr.judge_system.VJ.Submitter.VJSubmitter;
import com.fjut.zr.judge_system.VJ.Submitter.VJSubmitterImpl;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("/submit")
@CrossOrigin
public class SubmitController {
    OJSubmitter ojSubmitter = new OJSubmitterImpl();
    VJSubmitter vJSubmitter = new VJSubmitterImpl();
    @RequestMapping("/oj")
    @LogAnnotation(module = "OJ提交代码", operator = "OJ提交代码")
    public ResultJson ojsubmitproblem(@RequestParam("ojid") int ojid,
                                      @RequestParam("rid") String rid,
                                      @RequestParam("pid") String pid,
                                      @RequestParam("language") String language,
                                      @RequestParam("code") String code,
                                      @RequestParam("time") int time,
                                      @RequestParam("memory") int memory) throws IOException {
        ResultJson resultJson = new ResultJson();
        code = new String(new BASE64Decoder().decodeBuffer(code), "utf-8");
        if (ojid == 0) {
            CodeLanguage codeLanguage = new CodeLanguage();
            String submitlanguage = codeLanguage.getLanguage(Integer.parseInt(language));
            String[] OJName = OJSubmitter.oj_keys;
            OJSubmitinfo ojSubmitinfo;
            System.out.println(code);
            int ojudgeId = -1;
            for (int i = 0; i < OJName.length; i++) {
                if (OJName[i].equals(submitlanguage) == true) {
                    ojudgeId = i;
                    break;
                }
            }
            if (ojudgeId == -1) {
                String rt = "failed";
                resultJson.setMsg(rt);
                return resultJson;
            }
            System.out.println("=========OJ收到信息==========");
            System.out.println("RunID:" + rid);
            System.out.println("测评机ID:" + ojudgeId);
            System.out.println("的题目ID:" + pid);
            System.out.println("编程语言:" + submitlanguage);
            System.out.println("============================");
            //获取一个Date对象
            Date date = new Date();
            //初始化测评状态
            ojSubmitinfo = new OJSubmitinfo(rid, ojudgeId, pid, code,time,memory);
            ojSubmitter.doSubmit(ojSubmitinfo);
            String rt = "success";
            resultJson.setMsg(rt);
            return resultJson;
        } else {
            CodeLanguage codeLanguage = new CodeLanguage();
            String submitlanguage = codeLanguage.getLanguage(Integer.parseInt(language));
            String OJName = VJSubmitter.vj_keys[ojid];
            System.out.println("=========VJ收到信息==========");
            System.out.println("原OJ的RunID:" + rid);
            System.out.println("目标OJ的ID:" + ojid);
            System.out.println("目标OJ的题目ID:" + pid);
            System.out.println("编程语言ID:" + submitlanguage);
            System.out.println("目标OJ:" + OJName);
            System.out.println("==========================");
            TVjSubmitPO submitInfoPO = new TVjSubmitPO(rid, ojid, pid, submitlanguage, code, 0, new Timestamp(System.currentTimeMillis()));
            vJSubmitter.doSubmit(submitInfoPO);
            String rt = "success";
            resultJson.setMsg(rt);
            return resultJson;
        }
    }
}
